/*
 * Copyright (C) 2009 M. Hautle.
 * 
 * This file is part of TrackedBean.
 * 
 * TrackedBean is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * TrackedBean is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with TrackedBean. If not, see <http://www.gnu.org/licenses/>.
 */
package ch.trackedbean.copier.engine;

import java.beans.*;
import ch.trackedbean.common.*;
import ch.trackedbean.copier.*;
import ch.trackedbean.util.*;

/**
 * Object holding a flat mapping.
 * 
 * @author M. Hautle
 */
public class SimpleMappingHolder extends AbstractSimpleMappingHolder {
	/** The property convertor to use. */
	protected final IPropertyConverter converter;

	/**
	 * Default constructor.
	 * 
	 * @param el The expression
	 * @param dstProp The destination property (on the object holding the mapping annotations)
	 * @param converter The property converter to use or null
	 * @param readOnly Flag indicating if the mapping should be 'readonly' so that {@link #copyDst2Src(Object, Object, IMappingContext)} will not be executed
	 * @param lazy Flag indicating if the property should be fetched lazy
	 */
	public SimpleMappingHolder(String el, PropertyDescriptor dstProp, IPropertyConverter converter, boolean readOnly, boolean lazy) {
		super(el, null, dstProp, readOnly, lazy);
		this.converter = converter != null ? converter : NOPPropertyConverter.NOP;
	}

	/**
	 * Default constructor.
	 * 
	 * @param src The source property
	 * @param dst The destination property (on the object holding the mapping annotations)
	 * @param converter The property converter to use or null
	 * @param readOnly Flag indicating if the mapping should be 'readonly' so that {@link #copyDst2Src(Object, Object, IMappingContext)} will not be executed
	 * @param lazy Flag indicating if the property should be fetched lazy
	 */
	public SimpleMappingHolder(PropertyDescriptor src, PropertyDescriptor dst, IPropertyConverter converter, boolean readOnly, boolean lazy) {
		super(src, dst, readOnly, lazy);
		this.converter = converter != null ? converter : NOPPropertyConverter.NOP;
	}

	/**
	 * Default constructor. {@link SelfAccessor} will be used to retrive the source object. This mapping will be read only!
	 * 
	 * @param dst The destination property
	 * @param converter The property converter to use or null
	 * @param lazy Flag indicating if the property should be fetched lazy
	 */
	public SimpleMappingHolder(PropertyDescriptor dst, IPropertyConverter converter, boolean lazy) {
		super(SelfAccessor.ME, new PropertyAccessor(dst), true, lazy);
		this.converter = converter != null ? converter : NOPPropertyConverter.NOP;
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public void copyToDestination(Object src, Object dst, IMappingContext context) {
		try {
			final Object val = srcProp.getValue(src, UNDEFINED);
			if (val != UNDEFINED)
				dstProp.setValue(dst, converter.convertSrc2Dst(val, dstProp.getType(), context));
		} catch (Exception e) {
			throw new TechnicalException("Error while copying " + this.srcProp.getPath() + " to " + this.dstProp.getPath(), e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public void copyToSource(Object src, Object dst, IMappingContext context) {
		try {
			final Object val = dstProp.getValue(dst, UNDEFINED);
			// do nothing if there's no way to copy the value
			if (val != UNDEFINED)
				srcProp.setValue(src, converter.convertDst2Src(val, srcProp.getType(), context));
		} catch (Exception e) {
			throw new TechnicalException("Error while copying " + this.dstProp.getPath() + " to " + this.srcProp.getPath(), e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Object getSrcValue(Object src) {
		return converter.convertSrc2Dst(super.getSrcValue(src), dstProp.getType(), NOPMappingContext.NOP);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Object getDstValue(Object dst) {
		return converter.convertDst2Src(super.getDstValue(dst), srcProp.getType(), NOPMappingContext.NOP);
	}
}